<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Requests\Api\ApiRequest as Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Interfaces\RefundGoodsService;
use App\Interfaces\RefundGoodsOperationRecordService;

use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\RefundGoods;
use App\Models\RefundGoodsOperationRecord;

use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Exceptions\Interfaces\RefundGoodsServiceException;

use App\Commands\OperationLogCommand;
use App\Interfaces\OrderService;

class RefundGoodsController extends Controller
{

    /**
     * @api {post} /refund/create_return_goods 创建售后服务单
     * @apiName Refund/create_return_goods
     * @apiGroup Refund
     * @apiDescription 创建售后服务单.
     * @apiParam {String} order_sn  订单编号.
     * @apiParam {Number} goods_id  商品id.
     * @apiParam {Number} after_sales_number  要申请售后的商品数量.
     * @apiParam {String} consignee  联系人姓名.
     * @apiParam {String} phone  联系人电话.
     * @apiParam {String} [reason]  售后（退/换货）原因.
     * @apiParam {String} [remark]  备注信息.
     *
     * @apiSuccess {String}  refund_sn      售后单编号
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //售后申请成功
     * {
     *   "refund_sn":"R4114459564710d"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //售后申请失败
     * {
     *   "message": "商品id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //售后申请失败
     * {
     *   "message": "订单编号不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //售后申请失败
     * {
     *   "message": "数量不能为空"
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 400
     * //售后申请失败
     * {
     *   "message": "联系人不能为空"
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 400
     * //售后申请失败
     * {
     *   "message": "手机号码不能为空"
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //售后申请失败
     * {
     *   "message": "该商品已经申请过售后"
     * }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 202
     * //售后申请失败
     * {
     *   "message": "手机号必须跟下单时所用一样"
     * }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 202
     * //售后申请失败
     * {
     *   "message": "该商品在订单中不存在"
     * }
     */
    public function createReturnGoods(Request $request, RefundGoodsService $refundGoodsService, RefundGoodsOperationRecordService $refundGoodsOperationRecordService)
    {

        $goods_id = trim($request->goods_id);                     //商品id
        $order_sn = trim($request->order_sn);                     //订单编号
        $number = trim($request->after_sales_number);             //售后数量
        $real_name = trim($request->consignee);                   //联系人
        $phone = trim($request->phone);                           //手机号码
        $reason = trim($request->reason);                         //售后（退/换货）原因
        $remark = trim($request->remark);                         //备注信息

        $user_id = $request->apiUser->get()->id;

        if(empty($goods_id)){

            throw new ApiInputErrorException("商品id不能为空");
        }//end if()

        if(empty($order_sn)){

            throw new ApiInputErrorException("订单编号不能为空");
        }//end if()

        if(empty($number)){

            throw new ApiInputErrorException("数量不能为空");
        }//end if()

        if(empty($real_name)){

            throw new ApiInputErrorException("联系人不能为空");
        }//end if()

        if(empty($phone)){

            throw new ApiInputErrorException("手机号码不能为空");
        }//end if()

        $isHas = RefundGoods::where('order_sn', $order_sn)->where('goods_id', $goods_id)->first();

        if(!empty($isHas)){

            throw new ApiDoneErrorException("该商品已经申请过售后");
        }//end if()


        $orders = Order::where('order_sn', $order_sn)->first();
        $order_status = $orders->order_status;
        $order_id = $orders->order_id;
        $mobile = $orders->mobile;

        if(trim($order_status) != 4){

            throw new ApiDoneErrorException("只有已完成的订单才能够申请售后");
        }//end if()

        if(trim($phone) !== trim($mobile)){

            throw new ApiDoneErrorException("手机号必须跟下单时所用一样");
        }//end if()

        $order_goods = OrderGoods::where('order_id', $order_id)->where('goods_id', $goods_id)->first();
        if (empty($order_goods)) {

            throw new ApiDoneErrorException("该商品在订单中不存在");
        }
        $goods_sn = $order_goods->goods_sn;

        $order_goods_id = $order_goods->id;
        $shop_price = $order_goods->shop_price;
        $goods_real_price = $order_goods->goods_real_price;

        try{

            $refund_id = $refundGoodsService->createReturnGoods($user_id, $order_sn, $goods_id, $order_goods_id, $goods_sn, $number, $real_name, $phone, $shop_price, $goods_real_price, $reason, $remark, $refundGoodsOperationRecordService);

            $return['data'] = $refund_id;
            $refund_sn = RefundGoods::where('refund_id', $refund_id)->pluck('refund_sn');

            /*$this->dispatch(
                new OperationLogCommand('用户申请售后', 'refund_goods','refund_id:'.$refund_id.';order_id:'.$order_id.';goods_id:'.$goods_id)
            );*/

            return response()->json(['refund_sn'=>$refund_sn]);

        } catch (RefundGoodsServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());

        }//end try


    }//end func createReturnGoods()

    /**
     * @api {post} /refund/get_by_refund_sn 根据售后单号查询售后详情
     * @apiName Refund/get_by_refund_sn
     * @apiGroup Refund
     * @apiDescription 根据售后单号查询售后详情.
     * @apiParam {Number} refund_sn  售后单号.
     *
     * @apiSuccess {Object[]}  refund_goods      售后订单信息
     * @apiSuccess {Number}  refund_id      售后订单id
     * @apiSuccess {String}  refund_sn      售后订单编号
     * @apiSuccess {String}  order_sn       订单编号
     * @apiSuccess {Number}  goods_id       商品id
     * @apiSuccess {Number}  order_goods_id      订单商品表id
     * @apiSuccess {String}  goods_sn       商品编号
     * @apiSuccess {Number}  number         商品售后数量
     * @apiSuccess {Number}  refund_status      退货商品的售后服务状态：1:待确认; 2:已受理；3:处理完毕；4:不予受理；5:已取消；
     * @apiSuccess {Number}  user_id        用户id
     * @apiSuccess {String}  real_name      联系人名称
     * @apiSuccess {String}  phone          联系人电话
     * @apiSuccess {Number}  shop_price     商品价格
     * @apiSuccess {String}  reason         售后原因
     * @apiSuccess {String}  remark         用户备注信息
     * @apiSuccess {String}  country        国家
     * @apiSuccess {String}  province       省份
     * @apiSuccess {String}  city           城市
     * @apiSuccess {String}  district       街道
     * @apiSuccess {String}  address        详细地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //售后订单详情
     * {
     *   "refund_id": 3,
     *   "refund_sn": "R4114459564710d",
     *   "order_sn": "G4079525113862d",
     *   "goods_id": 1346,
     *   "order_goods_id": 111,
     *   "goods_sn": "010101",
     *   "number": 1,
     *   "refund_status": 1,
     *   "user_id": 59,
     *   "real_name": "李宁",
     *   "phone": "17737132820",
     *   "shop_price": "99.00",
     *   "reason": "",
     *   "remark": "",
     *   "country": "",
     *   "province": "",
     *   "city": "",
     *   "district": "",
     *   "address": ""
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //查询失败
     * {
     *   "message": "售后单号不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //查询失败
     * {
     *   "message": "售后单号不存在或与当前用户不匹配"
     * }
     */
    public function getByRefundSn(Request $request)
    {
        $refund_sn = $request->refund_sn;
        if(empty($refund_sn)){

            throw new ApiInputErrorException("售后单号不能为空");
        }//end if()

        $user_id = $request->apiUser->get()->id;
        $refund_goods = RefundGoods::where('refund_sn', $refund_sn)->where('user_id', $user_id)->first();

        if($refund_goods){

            return response()->json($refund_goods);
        }else{

            throw new ApiDoneErrorException("售后单号不存在或与当前用户不匹配");
        }//end if()


    }//end func getByRefundSn()


    /**
     * @api {post} /refund/get_by_order_sn_goods_sn 根据订单号和订单商品编码获取服务单信息
     * @apiName Refund/get_by_order_sn_goods_sn
     * @apiGroup Refund
     * @apiDescription 根据订单号和订单商品编码获取服务单信息.
     * @apiParam {String} order_sn  订单编号.
     * @apiParam {String} goods_sn  商品编号.
     *
     * @apiSuccess {Object[]}  refund_goods      售后订单信息
     * @apiSuccess {Number}  refund_id      售后订单id
     * @apiSuccess {String}  refund_sn      售后订单编号
     * @apiSuccess {String}  order_sn       订单编号
     * @apiSuccess {Number}  goods_id       商品id
     * @apiSuccess {Number}  order_goods_id      订单商品表id
     * @apiSuccess {String}  goods_sn       商品编号
     * @apiSuccess {Number}  number         商品售后数量
     * @apiSuccess {Number}  refund_status      退货商品的售后服务状态：1:待确认; 2:已受理；3:处理完毕；4:不予受理；5:已取消；
     * @apiSuccess {Number}  user_id        用户id
     * @apiSuccess {String}  real_name      联系人名称
     * @apiSuccess {String}  phone          联系人电话
     * @apiSuccess {Number}  shop_price     商品价格
     * @apiSuccess {String}  reason         售后原因
     * @apiSuccess {String}  remark         用户备注信息
     * @apiSuccess {String}  country        国家
     * @apiSuccess {String}  province       省份
     * @apiSuccess {String}  city           城市
     * @apiSuccess {String}  district       街道
     * @apiSuccess {String}  address        详细地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //售后订单详情
     * {
     *   "refund_id": 2,
     *   "refund_sn": "R4114450645899d",
     *   "order_sn": "G4079525113862d",
     *   "goods_id": 1340,
     *   "order_goods_id": 111,
     *   "goods_sn": "010101",
     *   "number": 1,
     *   "refund_status": 1,
     *   "user_id": 59,
     *   "real_name": "李宁",
     *   "phone": "17737132820",
     *   "shop_price": "99.00",
     *   "reason": "",
     *   "remark": "",
     *   "country": "",
     *   "province": "",
     *   "city": "",
     *   "district": "",
     *   "address": ""
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //查询失败
     * {
     *   "message": "订单号不能为空"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //查询失败
     * {
     *   "message": "商品编号不能为空"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 202
     * //查询失败
     * {
     *   "message": "订单中的商品没有售后记录"
     * }
     */
    public function getByOrderSnGoodsSn(Request $request){

        $order_sn = $request->order_sn;
        $goods_sn = $request->goods_sn;

        if(empty($order_sn)){

            throw new ApiInputErrorException("订单号不能为空");
        }//end if()
        if(empty($goods_sn)){

            throw new ApiInputErrorException("商品编号不能为空");
        }//end if()

        $refundGoods = RefundGoods::where('order_sn', $order_sn)->where('goods_sn', $goods_sn)->first();
        if ($refundGoods) {

            return response()->json($refundGoods);
        }else{

            throw new ApiDoneErrorException("订单中的商品没有售后记录");
        }//end if()

    }//end func getByRefundSn()

    /**
     * @api {post} /refund/get_order_goods_refund_info_by_order_id 根据订单id获取订单、订单商品以及订单商品是否可以售后和售后状态
     * @apiName Refund/get_order_goods_refund_info_by_order_id
     * @apiGroup Refund
     * @apiDescription 根据订单id获取订单、订单商品以及订单商品是否可以售后和售后状态.
     *
     * @apiParam {Number} order_id   订单id
     *
     * @apiSuccess {Number}  Object      订单信息
     *
     * @apiSuccess {number}  Object.order_id      订单id
     * @apiSuccess {String}  Object.order_sn      订单编号
     * @apiSuccess {number}  Object.user_id       用户id
     * @apiSuccess {number}  Object.order_status  订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiSuccess {number}  Object.order_type    订单类型，0：一般商品；1：e贸易商品
     * @apiSuccess {number}  Object.shop_id         店铺id
     * @apiSuccess {number}  Object.order_price         订单价格
     * @apiSuccess {number}  Object.order_tariff_fee         订单关税价格
     * @apiSuccess {number}  Object.shipping_fee         邮费
     * @apiSuccess {number}  Object.total_goods_number         商品总数
     * @apiSuccess {number}  Object.pay_price         支付价格
     * @apiSuccess {String}  Object.country         国家
     * @apiSuccess {String}  Object.province        省份
     * @apiSuccess {String}  Object.city            城市
     * @apiSuccess {String}  Object.district        区
     * @apiSuccess {String}  Object.address         详细地地址
     * @apiSuccess {String}  Object.consignee       联系人
     * @apiSuccess {String}  Object.mobile          联系人电话
     * @apiSuccess {String}  Object.id_type          证件类型：默认身份证（1：身份证）
     * @apiSuccess {String}  Object.id_card         身份证号码
     * @apiSuccess {String}  Object.invoice_message         用户发票信息
     * @apiSuccess {String}  Object.order_message         订单备注信息
     * @apiSuccess {String}  Object.deleted_at         删除时间
     * @apiSuccess {String}  Object.add_time         下单时间
     * @apiSuccess {String}  Object.shipping_type         配送方式
     * @apiSuccess {String}  Object.shipping_num         运单号
     * @apiSuccess {String}  Object.shipping_time         发货时间
     * @apiSuccess {String}  Object.shipping_message         用户自定义发货信息
     * @apiSuccess {number}  Object.order_pay_id         支付ID 关联order_pay 主键
     * @apiSuccess {number}  Object.push_tarrif         是否已经推关，0：未推关；1：已推关
     * @apiSuccess {String}  Object.push_tarrif_time         推关时间
     * @apiSuccess {String}  Object.recive_goods_time         收货时间
     * @apiSuccess {String}  Object.cancel_reason         取消订单原因
     * @apiSuccess {String}  Object.cancel_time         取消订单时间
     * @apiSuccess {number}  Object.is_remind         是否提醒发货：0：不提醒；1：提醒
     * @apiSuccess {number}  Object.pay_push         支付推关结果：1、成功；2、失败
     * @apiSuccess {number}  Object.pay_push_success_time         支付推关成功的时间
     * @apiSuccess {Object[]}  Object.orderGoods         订单商品信息
     * @apiSuccess {number}  Object.order_goods.id         订单商品表主键id
     * @apiSuccess {number}  Object.order_goods.order_id         订单id
     * @apiSuccess {Number}  Object.order_goods.goods_id                商品id
     * @apiSuccess {String}  Object.order_goods.goods_sn                商品编号
     * @apiSuccess {String}  Object.order_goods.goods_name              商品名称
     * @apiSuccess {String}  Object.order_goods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.order_goods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.order_goods.buy_number              商品购买数量
     * @apiSuccess {Number}  Object.order_goods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.order_goods.shop_price              商品商城价格
     * @apiSuccess {Number}  Object.order_goods.tariff_percent          关税比例
     * @apiSuccess {String}  Object.order_goods.goods_thumb             商品缩略图
     * @apiSuccess {Number}  Object.order_goods.shop_id                 店铺id
     * @apiSuccess {Number}  Object.order_goods.cat_id                  分类id
     * @apiSuccess {Number}  Object.order_goods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.order_goods.comment_status          评论状态，0：未评论；1：已评论；
     * @apiSuccess {Number}  Object.order_goods.brand_id          品牌id
     * @apiSuccess {Number}  Object.order_goods.goods_content           商品内容
     * @apiSuccess {Number}  Object.order_goods.resource           商品来源
     * @apiSuccess {Number}  Object.order_goods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.order_goods.goods_spec
     * @apiSuccess {Number}  Object.order_goods.price                   备案价格
     * @apiSuccess {Number}  Object.order_goods.ieType                  进出口标志 I-进口  E-出口
     * @apiSuccess {Number}  Object.order_goods.stockFlag               集货/备货 1-集货 2-备货
     * @apiSuccess {Number}  Object.order_goods.batchNumbers            批次号
     * @apiSuccess {Number}  Object.order_goods.tradeCountryCiq         贸易国别（检） 进口是发货人国家的国检编码，出口是收货人国家的国检编码（参考数据字典国检的国家代码）【配置】
     * @apiSuccess {Number}  Object.order_goods.tradeCountryCus         贸易国别（关） 进口是发货人国家的海关编码，出口是收货人国家的海关编码（参考数据字典海关的国家代码）【配置】
     * @apiSuccess {Number}  Object.order_goods.packageTypeCiq          包装种类（检）
     * @apiSuccess {Number}  Object.order_goods.packageTypeCus          包装种类（关）（参考数据字典的海关包装种类代码）
     * @apiSuccess {Number}  Object.order_goods.currencyCiq             币制（检）配置
     * @apiSuccess {Number}  Object.order_goods.currencyCus             币制（关）配置
     * @apiSuccess {Number}  Object.order_goods.unitCiq                 计量单位（关）
     * @apiSuccess {Number}  Object.order_goods.unitCus                 计量单位（关）
     * @apiSuccess {Number}  Object.order_goods.originCountryCiq        原产国（关）【配置】
     * @apiSuccess {Number}  Object.order_goods.originCountryCus        原产国（关）【配置】
     * @apiSuccess {Number}  Object.order_goods.weight                  毛重(KG)
     * @apiSuccess {Number}  Object.order_goods.transportationWay       运输方式（检）【配置】
     * @apiSuccess {Number}  Object.order_goods.shipCode                运输工具（检）【配置】
     * @apiSuccess {Number}  Object.order_goods.checkStatus             审核状态：0：未审核；1：已审核
     * @apiSuccess {Number}  Object.order_goods.is_refund               该订单是商品是否可以申请退货：0：不可以；1：可以
     * @apiSuccess {Number}  Object.order_goods.refund_status           退货商品的售后服务状态：1:待确认; 2:已受理；3:处理完毕；4:不予受理；5:已取消；（为0表示没有申请过售后）
     * @apiSuccess {String}  Object.order_goods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单查询成功
     * {
     *   "order_id": 137,
     *   "order_sn": "G4087920804837d",
     *   "user_id": 59,
     *   "order_status": 1,
     *   "order_type": 1,
     *   "shop_id": 1,
     *   "order_price": "88.00",
     *   "order_tariff_fee": "0.00",
     *   "shipping_fee": "0.00",
     *   "total_goods_number": 3,
     *   "pay_price": "88.00",
     *   "country": "中国",
     *   "province": "北京市",
     *   "city": "北京市",
     *   "district": "石景山区",
     *   "address": "西城路22号",
     *   "consignee": "李宁",
     *   "mobile": "17737132820",
     *   "id_type": "1",
     *   "id_card": "411024198903052223",
     *   "invoice_message": null,
     *   "order_message": "",
     *   "deleted_at": null,
     *   "add_time": "2016-04-08 09:33:28",
     *   "shipping_type": "",
     *   "shipping_num": "",
     *   "shipping_time": null,
     *   "shipping_message": "",
     *   "order_pay_id": 0,
     *   "push_tarrif": 0,
     *   "push_tarrif_time": null,
     *   "recive_goods_time": null,
     *   "cancel_reason": "订单超时,系统自动取消",
     *   "cancel_time": "2016-04-08 09:33:28",
     *   "is_remind": 0,
     *   "pay_push": null,
     *   "pay_push_success_time": null,
     *   "id": 112,
     *   "goods_id": 1342,
     *   "goods_sn": "aa",
     *   "goods_name": "净白霜",
     *   "goods_pet_name": "aa",
     *   "goods_desc": "aa",
     *   "buy_number": 1,
     *   "goods_weight": "0.000",
     *   "shop_price": "22.00",
     *   "tariff_percent": 50,
     *   "goods_thumb": "",
     *   "cat_id": 393,
     *   "type": 1,
     *   "comment_status": 0,
     *   "brand_id": 77,
     *   "goods_content": "",
     *   "resource": "aa",
     *   "orderGoods": [
     *     {
     *       "id": 112,
     *       "order_id": 137,
     *       "goods_id": 1342,
     *       "goods_sn": "aa",
     *       "goods_name": "净白霜",
     *       "goods_pet_name": "aa",
     *       "goods_desc": "aa",
     *       "buy_number": 1,
     *       "goods_weight": "0.000",
     *       "shop_price": "22.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "",
     *       "shop_id": 1,
     *       "cat_id": 393,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 77,
     *       "goods_content": "",
     *       "resource": "aa",
     *       "add_time": 1460079208,
     *       "goods_spec": null,
     *       "price": null,
     *       "ieType": null,
     *       "stockFlag": null,
     *       "batchNumbers": null,
     *       "tradeCountryCiq": null,
     *       "tradeCountryCus": null,
     *       "packageTypeCiq": null,
     *       "packageTypeCus": null,
     *       "currencyCiq": null,
     *       "currencyCus": null,
     *       "unitCiq": null,
     *       "unitCus": null,
     *       "originCountryCiq": null,
     *       "originCountryCus": null,
     *       "weight": null,
     *       "transportationWay": null,
     *       "shipCode": null,
     *       "checkStatus": null,
     *       "is_refund": 1,
     *       "refund_status": 2,
     *       "mobile_goods_thumb": ""
     *     },
     *     {
     *       "id": 113,
     *       "order_id": 137,
     *       "goods_id": 1343,
     *       "goods_sn": "33",
     *       "goods_name": "兰蔻",
     *       "goods_pet_name": "33",
     *       "goods_desc": "33",
     *       "buy_number": 2,
     *       "goods_weight": "0.000",
     *       "shop_price": "33.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "7635ac27ee5da6f8b6358c5a2c2188ae",
     *       "shop_id": 1,
     *       "cat_id": 398,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 74,
     *       "goods_content": "",
     *       "resource": "33",
     *       "add_time": 1460079208,
     *       "goods_spec": null,
     *       "price": null,
     *       "ieType": null,
     *       "stockFlag": null,
     *       "batchNumbers": null,
     *       "tradeCountryCiq": null,
     *       "tradeCountryCus": null,
     *       "packageTypeCiq": null,
     *       "packageTypeCus": null,
     *       "currencyCiq": null,
     *       "currencyCus": null,
     *       "unitCiq": null,
     *       "unitCus": null,
     *       "originCountryCiq": null,
     *       "originCountryCus": null,
     *       "weight": null,
     *       "transportationWay": null,
     *       "shipCode": null,
     *       "checkStatus": null,
     *       "is_refund": 0,
     *       "refund_status": 0,
     *       "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/8a75e0739bad792a326d8153dc6306fe.jpg"
     *     }
     *   ]
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单查询失败
     * {
     *   "message": "订单id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //订单查询失败
     * {
     *   "message": "订单不存在"
     * }
     */
    public function getOrderGoodsRefundInfoByOrderId(Request $request, OrderService $orderService)
    {

        $order_id = $request->order_id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()


        try {

            $orders = $orderService->getByOrderId($order_id);

            $order_sn = $orders->order_sn;

            foreach ($orders['orderGoods'] as $key => $value) {

                $orders['orderGoods'][$key]['is_refund'] = 1;

                $orders['orderGoods'][$key]['refund_status'] = 0;

                $refundGoods = RefundGoods::where('order_sn', $order_sn)->where('goods_sn', $value->goods_sn)->first();

                if ($refundGoods) {

                    $orders['orderGoods'][$key]['is_refund'] = 0;

                    $orders['orderGoods'][$key]['refund_status'] = $refundGoods->refund_status;
                }//end if()

            }

            return response()->json($orders);
        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

    }


}
